package community.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import community.dal.DatabaseSession;
import community.dao.ITopicDao;
import community.domain.Topic;
import community.exception.DatabaseException;

public class TopicDao implements ITopicDao {

	@Override
	public ArrayList<Topic> getTopic(DatabaseSession databaseSession) throws DatabaseException {
		String sql = "SELECT * FROM topic";
		ArrayList<Topic> topics = new ArrayList<>();
		try {
			ResultSet rs = databaseSession.executeQuery(sql);
			while (rs.next()) {
				Topic topic = new Topic();
				topic.setTid(rs.getInt("tid"));
				topic.setTopicname(rs.getString("topicname"));
				topic.setContent(rs.getString("content"));
				topic.setImg(rs.getString("img"));
				topics.add(topic);
			}
			return topics;
		} catch (SQLException e) {
			e.printStackTrace();
			throw new DatabaseException(e);
		}
	}

}
